﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace HMI_BR_tech
{
    class commonfunc
    {
        static List<object> listobject = new List<object>();
        static string strProRunPath = System.IO.Directory.GetCurrentDirectory();//应用程序的当前路径
        
        public static int createXmlFile(string strXmlFile)
        {
            //FormSystemManagement.strProRunPath
            XDocument xdoc = new XDocument();

            //创建根节点
            XElement root = new XElement("user");
            //添加根节点
            xdoc.Add(root);

            //
            XElement per = new XElement("person");
            root.Add(per);
            XElement per1 = new XElement("person");
            root.Add(per1);

            //创建属性节点
            XAttribute xId = new XAttribute("id", "203");

            XElement name = new XElement("name", "Tom");
            XElement key = new XElement("key", "000");
            XElement permission = new XElement("permission", "0");
            XElement delay = new XElement("delay", "0");

            per.Add(xId, name, key, permission);

            XAttribute xId1 = new XAttribute("id", "109");

            XElement name1 = new XElement("name", "Mary");
            XElement key1 = new XElement("key", "123");
            XElement permission1 = new XElement("permission", "1");
            XElement delay1 = new XElement("delay", "5");

            per1.Add(xId1, name1, key1, permission1);
            
            xdoc.Save(strProRunPath + "/" + strXmlFile);

            return 0;
        }
        public int addXmlItem(string strItem,string strValue)
        {
            return 0;
        }
        public int deleteXmlItem(string strItem)
        {
            return 0;
        }
        public static int loadXmlFile(List<object> list,string strXmlFile)
        {
            XDocument xdoc = XDocument.Load(strProRunPath + "/" + strXmlFile);
            XElement rootNode = xdoc.Root;
            rootNode.Name.ToString();//通过根节点名，区别各个表

            foreach (XElement node in rootNode.Elements())
            {
                if (node.Elements().Count() <= 0)
                {
                    return 0;
                }
                string name = node.Element("name").Value;
                Console.WriteLine(name);
                list.Add(name);
                

            }

            //foreach(xdoc.)
            //listobject
            //xdoc.DocumentType                                                                                                                                                                                     

            return 0;
        }

    }
}
